function General(){
	this.table=this.InitTable();
    this.tab=this.InitTab();
	this.form=this.InitEleUI();
}

General.prototype.InitEleUI=function(){
	var form;
	layui.use('form', function(){
       form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
	   form.render();
	});  
	return form;
}
General.prototype.InitTab=function(){
	var element;
	layui.use('element', function(){
	   element = layui.element;
	});
	return element;
}

General.prototype.OpTab=function(tabindex,obj){
	this.tab.tabChange('tabcontent', tabindex);
	document.formedit.reset();
	if(obj){
		 document.formedit.parentid.value=obj.parentid;
		 document.formedit.generalcode.value=obj.generalcode;
		 document.formedit.codename.value=obj.codename;
		 document.formedit.generalid.value=obj.generalid;
		 document.formedit.codedes.value=obj.codedes;
		 
		 $("input[name='enableflag']").removeAttr("checked");
		 $("input[name='enableflag']").eq(obj.enableflag).attr("checked","");
		 this.form.render();
	}
}

General.prototype.InitTable = function() {
	var table;
	var _this=this;
	layui.use('table', function() {
	    table = layui.table;
		table.render({
			elem : '#list'
			,height : $(window).height() - 120
			,url : 'get_list' // 数据接口
			,page : true // 开启分页
			,limit : 30
			,id: 'generallist'
			,method:"post"
			,cols : [ [ // 表头
			           {type:'numbers'}
			           ,{field : 'codename',title : '名称',width : 150,align : 'left'}
			           , {field : 'generalcode',title : '编码 ',width : 70,align : 'center'}
			           , {field : 'parentcode',title : '上级编码 ',width : 100,align : 'center'}
			           , {field : 'createtime',title : '时间',width : 170,align : 'center'}
			           //, {field : 'keyword',title : '关键字',width : 100,align : 'center'}
			           , {field : 'codedes',title : '描述',width : 180,align : 'center'}
			           , {title : '操作',width : 100,align : 'center',toolbar : '#operate'}
		            ] ]
		    ,request:{
				type:'post'
			}
		    ,even: true //开启隔行背景
			,where:{rolename: $("#rolename").val()}
		    ,done: function(res, page, count){
		    	$("[data-field='codename']").children().each(function(index){ 
	            	   var dataarr=res.data;
	            	   if(index==0){
			    			$(this).css({"text-align":"center"});
			    		}
			    		var thisdata=dataarr[index-1];
			    		if(thisdata){
			    			var text=$(this).text();
			                $(this).css({"text-indent":thisdata.codelevel+"em"});
			    		}
	              })  
		    }
		    
		});

		
		 //监听工具条
	    table.on('tool(list)', function(obj){
	      var data = obj.data;
	      if(obj.event === 'detail'){
	        layer.msg('ID：'+ data.id + ' 的查看操作');
	      } else if(obj.event === 'del'){
	    	  
	        top.mylayui.confirm('确定要删除吗？',{icon: 0}, function(index,dd){
	        	 top.mylayui.close(index);
	        	 vGeneral.deleteentity(data.generalid);
	        });
	      } else if(obj.event === 'edit'){
	        //layer.alert('编辑行：<br>'+ JSON.stringify(data))
	    	  _this.OpTab(2,data);
	      }
	    });

	    
	});
	
	return table;
}

General.prototype.searchtable=function(){
	this.table.reload('generallist', { 
		  page: {
	        curr: 1 //重新从第 1 页开始
	      }
	    ,where: {  
	  	  rolename: $("#rolename").val()  
	    }  
	}); 
}


General.prototype.deleteentity=function(generalid){
	$.ajax({
		type:"POST",
		url:"delete_generalcode",
		data:{generalid:generalid},
		beforeSend:function(XHR){
			
		},
		success:function(data){
			data=eval('('+data+')');
			if(data.result>0){
				top.mylayui.msg(data.msg, {icon: 1});
				//vOrg.searchtable();
				document.location.reload();
			}
			else{
				top.mylayui.msg(data.msg, {icon: 0});
			}
		},
		complete:function(XHR){
			
		},
		error:function(){
			
		}
	});
}




var vGeneral;
$(function () {
	vGeneral = new General();
})


function CloseDialog(result,msg){
	if(result>0){
		top.mylayui.msg('操作成功', {icon: 1});
		document.formedit.reset();
		//vOrg.searchtable();
		//vOrg.OpTab(1);
		document.location.reload();
	}
	else{
		if(msg){
			top.mylayui.msg(msg, {icon: 0});
		}else{
			top.mylayui.msg('操作失败', {icon: 0});
		}
	}
}